package com.borqsprocessmanager.util;

import java.util.Map;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class PreferencesUtil {

    private Context mContext;

    public static PreferencesUtil mInstance = null;

    public static PreferencesUtil getInstance(Context context) {
        if (null == mInstance) {
            mInstance = new PreferencesUtil(context);
        }
        return mInstance;
    }

    private PreferencesUtil(Context context) {
        this.mContext = context;
    }

    /**
     * SharedPreferences ��ʼ��
     * 
     * @return
     */
    private SharedPreferences getSharedPreferences() {
        return mContext.getSharedPreferences("coupon",
                Context.MODE_MULTI_PROCESS);
    }

    /**
     * SharedPreferences �洢string�������
     * 
     * @param key
     * @param value
     */
    public void putString(String key, String value) {
        SharedPreferences sharedPreferences = getSharedPreferences();
        Editor editor = sharedPreferences.edit();
        editor.putString(key, value);
        editor.commit();
    }

    /**
     * SharedPreferences �洢int�������
     * 
     * @param key
     * @param value
     */
    public void putInt(String key, int value) {
        SharedPreferences sharedPreferences = getSharedPreferences();
        Editor editor = sharedPreferences.edit();
        editor.putInt(key, value);
        editor.commit();
    }

    /**
     * SharedPreferences �洢boolean�������
     * 
     * @param key
     * @param value
     */
    public void putBoolean(String key, Boolean value) {
        SharedPreferences sharedPreferences = getSharedPreferences();
        Editor editor = sharedPreferences.edit();
        editor.putBoolean(key, value);
        editor.commit();
    }

    public void putFloat(String key, float value) {
        SharedPreferences sharedPreferences = getSharedPreferences();
        Editor editor = sharedPreferences.edit();
        editor.putFloat(key, value);
        editor.commit();
    }

    public void putLong(String key, long value) {
        SharedPreferences sharedPreferences = getSharedPreferences();
        Editor editor = sharedPreferences.edit();
        editor.putLong(key, value);
        editor.commit();
    }

    public long getLong(String key, long defValue) {
        SharedPreferences sharedPreferences = getSharedPreferences();
        return sharedPreferences.getLong(key, defValue);
    }

    public float getFloat(String key, float defValue) {
        SharedPreferences sharedPreferences = getSharedPreferences();
        return sharedPreferences.getFloat(key, defValue);
    }

    /**
     * SharedPreferences ��ȡstring�������
     * 
     * @param key
     * @return
     */
    public String getString(String key) {
        SharedPreferences sharedPreferences = getSharedPreferences();
        return sharedPreferences.getString(key, "");
    }

    /**
     * SharedPreferences ��ȡint�������
     * 
     * @param key
     * @return
     */
    public int getInt(String key, int defValue) {
        SharedPreferences sharedPreferences = getSharedPreferences();
        return sharedPreferences.getInt(key, defValue);
    }

    /**
     * SharedPreferences ��ȡboolean�������
     * 
     * @param key
     * @return
     */
    public boolean getBoolean(String key) {
        SharedPreferences sharedPreferences = getSharedPreferences();
        return sharedPreferences.getBoolean(key, false);
    }

    /**
     * SharedPreferences ��ȡobject�������
     * 
     * @param key
     * @return
     */
    public Object getValue(String key) {
        SharedPreferences sharedPreferences = getSharedPreferences();
        Map<String, ?> map = sharedPreferences.getAll();
        if (map != null && !map.isEmpty()) {
            return map.get(key);
        }
        return null;
    }

}
